package com.zareal.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.zareal.domain.Application;
import com.zareal.dot.PutApplicationDto;
import com.zareal.query.ApplicationQuery;
import com.zareal.result.JSONResult;
import com.zareal.result.PageList;
import com.zareal.service.IApplicationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/application")
public class ApplicationController {

    @Autowired
    public IApplicationService applicationService;


    /**
     * 申请表
     */
    @RequestMapping(value="/putapplication",method= RequestMethod.POST)
    public JSONResult putApplication(@RequestBody PutApplicationDto putApplicationDto){
        JSONResult jsonResult = applicationService.putApplication(putApplicationDto);
        return JSONResult.success(jsonResult.getData());
    }
    /**
     * 采购
     */
    @RequestMapping(value="/purcharseproduct",method= RequestMethod.POST)
    public JSONResult purcharseProduct(@RequestBody PutApplicationDto putApplicationDto){
        JSONResult jsonResult = applicationService.putApplication(putApplicationDto);
        return JSONResult.success(jsonResult.getData());
    }
    /**
     * 审批通过
     */
    @RequestMapping(value="/passapplication/{id}",method= RequestMethod.GET)
    public JSONResult passApplication(@PathVariable Long id){
        JSONResult jsonResult = applicationService.passApplication(id);
        return JSONResult.success(jsonResult.getData());
    }
    /**
     * 审批失败
     */
    @RequestMapping(value="/failapplication/{id}",method= RequestMethod.GET)
    public JSONResult failApplication(@PathVariable Long id){
        JSONResult jsonResult = applicationService.failApplication(id);
        return JSONResult.success(jsonResult.getData());
    }

    /**
    * 保存和修改公用的
    */
    @RequestMapping(value="/save",method= RequestMethod.POST)
    public JSONResult saveOrUpdate(@RequestBody Application application){
        if(application.getId()!=null){
            applicationService.updateById(application);
        }else{
            applicationService.insert(application);
        }
        return JSONResult.success();
    }

    /**
    * 删除对象
    */
    @RequestMapping(value="/{id}",method=RequestMethod.DELETE)
    public JSONResult delete(@PathVariable("id") Long id){
        applicationService.deleteById(id);
        return JSONResult.success();
    }

    /**
   * 获取对象
   */
    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    public JSONResult get(@PathVariable("id")Long id){
        return JSONResult.success(applicationService.selectById(id));
    }


    /**
    * 查询所有对象
    */
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public JSONResult list(){
        return JSONResult.success(applicationService.selectList(null));
    }


    /**
    * 带条件分页查询数据
    */
    @RequestMapping(value = "/pagelist",method = RequestMethod.POST)
    public JSONResult page(@RequestBody ApplicationQuery query){
        Page<Application> page = new Page<Application>(query.getPage(),query.getRows());
        page = applicationService.selectPage(page);
        return JSONResult.success(new PageList<Application>(page.getTotal(),page.getRecords()));
    }
}
